﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using cfg = System.Configuration;
using System.Collections;
using System.Text.RegularExpressions;
using GoldPrinter;
using Maticsoft.DBUtility;//请先添加引用
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Excel;
namespace BYManageInfoSystem
{
    public partial class Frm_PostToBGSheetReg : DockContent
    {
        private DGBY.BLL.BYCompanyTab m_bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private DGBY.BLL.HaiGuanTab m_bllHaiGuanTab = new DGBY.BLL.HaiGuanTab();
        private DGBY.BLL.BGHDataTab m_bllBGHDataTab = new DGBY.BLL.BGHDataTab();
        private DGBY.BLL.T_ACL_User m_bllT_ACL_User = new DGBY.BLL.T_ACL_User();
        private DGBY.BLL.QsZzHxdTab m_bllQsZzHxdTab = new DGBY.BLL.QsZzHxdTab();
        private DGBY.BLL.JCHxdDjTab m_bllJCHxdDjTab = new DGBY.BLL.JCHxdDjTab();
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();

        private DataSet m_ds = new DataSet();
        private int m_nSelectQsZzHxdTabID = 0;
       


        List<string> m_UpdateList = new List<string>();
        public bool IsNull(ErrorProvider errorProvider1, ComboBox ComboBox)
        {
            if (ComboBox.Text == "")
            {
                errorProvider1.SetError(ComboBox, "内容不能为空！");
                return true;
            }
            else
            {
                errorProvider1.SetError(ComboBox, "");
                return false;
            }
        }
        public bool IsNull(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        public Frm_PostToBGSheetReg()
        {
            InitializeComponent();
        }

        private void Frm_PostToBGSheetReg_Load(object sender, EventArgs e)
        {
            InitCombox();
        }


        private void InitCombox()
        {
            DataSet ds = m_bllBYCompanyTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxCompName.DataSource = ds.Tables[0];
                this.comboBoxCompName.DisplayMember = "经营单位简称";
                this.comboBoxCompName.ValueMember = "BYCompanyTabID";
                this.comboBoxCompName.SelectedIndex = 0;
            }

            ds = m_bllHaiGuanTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxSeaGateName.DataSource = ds.Tables[0];
                this.comboBoxSeaGateName.DisplayMember = "海关简称";
                this.comboBoxSeaGateName.ValueMember = "HaiGuanTabID";
                this.comboBoxSeaGateName.SelectedIndex = 0;
            }

            ds = m_bllT_ACL_User.GetList("Name != 'System1'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxPoster.DataSource = ds.Tables[0];
                this.comboBoxPoster.DisplayMember = "用户简称";
                this.comboBoxPoster.ValueMember = "UserPasswordTabID";
                this.comboBoxPoster.SelectedIndex = 0;
            }

            ds = m_bllBGHDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxBGHName.DataSource = ds.Tables[0];

                this.comboBoxBGHName.ValueMember = "BGHDataTabID";
                this.comboBoxBGHName.DisplayMember = "报关行简称";
                this.comboBoxBGHName.SelectedIndex = 0;
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsNull(this.errorProvider1, this.textBoxStartNo))
                    return;

                long lStartNo = Convert.ToInt64(this.textBoxStartNo.Text, 10);
                long lEndNo = Convert.ToInt64(this.textBoxEndNo.Text, 10);
                long i64Cha = 0;
                if (lEndNo < lStartNo)
                {
                    MessageBox.Show("终止核销单号不能小于起始核销单号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    i64Cha = lEndNo - lStartNo + 1;
                    if (i64Cha > 2000)
                    {
                        MessageBox.Show("核销单号范围过大!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }

                DataSet ds = m_bllQsZzHxdTab.CheckHXDNo("HxdNo BETWEEN '" + this.textBoxStartNo.Text + "'" + " AND '" + this.textBoxEndNo.Text + "'");

                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string strHxdNoTabIDCount = ds.Tables[0].Rows[0]["HxdNoTabIDCount"].ToString();
                    int nHxdNoTabIDCount = Convert.ToInt32(strHxdNoTabIDCount, 10);
                    if (!(this.textBoxStartNo.Text == "812512936" && this.textBoxEndNo.Text == "812512942"))
                    {
                        if (nHxdNoTabIDCount > 0)
                        {
                            MessageBox.Show("核销单号重叠,请重新指定核销单号范围!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                }

                int nPaidanRegisterTabID = 0;
                ds = m_bllPaidanRegisterTab.GetList("ContractCode = ''");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    nPaidanRegisterTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["PaidanRegisterTabID"].ToString(), 10);
                }

                DGBY.Model.JCHxdDjTab modelJCHxdDjTab = new DGBY.Model.JCHxdDjTab();
                if (this.comboBoxCompName.SelectedValue != null)
                    modelJCHxdDjTab.BYCompanyTabID = Convert.ToInt32(this.comboBoxCompName.SelectedValue.ToString(), 10);
                if (this.comboBoxSeaGateName.SelectedValue != null)
                    modelJCHxdDjTab.HaiGuanTabID = Convert.ToInt32(this.comboBoxSeaGateName.SelectedValue.ToString(), 10);
                if (this.comboBoxBGHName.SelectedValue != null)
                    modelJCHxdDjTab.BGHDataTabID = Convert.ToInt32(this.comboBoxBGHName.SelectedValue.ToString(), 10);
                if (this.comboBoxPoster.SelectedValue != null)
                    modelJCHxdDjTab.UserPasswordTabID = Convert.ToInt32(this.comboBoxPoster.SelectedValue.ToString(), 10);

                modelJCHxdDjTab.JCDZDate = this.dateTimePickerSheetPostDate.Value;
                modelJCHxdDjTab.JCDZDate = modelJCHxdDjTab.JCDZDate.Date;
                ds = m_bllJCHxdDjTab.GetList("JCDZDate='" + modelJCHxdDjTab.JCDZDate + "'"
                                        + " AND BYCompanyTabID = " + modelJCHxdDjTab.BYCompanyTabID
                                        + " AND HaiGuanTabID=" + modelJCHxdDjTab.HaiGuanTabID
                                        + " AND BGHDataTabID=" + modelJCHxdDjTab.BGHDataTabID
                                        + " AND UserPasswordTabID=" + modelJCHxdDjTab.UserPasswordTabID);
                int nJCHxdDjTabID = 0;
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string strJCHxdDjTabID = ds.Tables[0].Rows[0]["JCHxdDjTabID"].ToString();
                    nJCHxdDjTabID = Convert.ToInt32(strJCHxdDjTabID, 10);
                }
                else
                {
                    nJCHxdDjTabID = m_bllJCHxdDjTab.Add(modelJCHxdDjTab);
                }

                List<string> arrayList = new List<string>();
                string strSQL = "";
                if (!(this.textBoxStartNo.Text == "812512936" && this.textBoxEndNo.Text == "812512942"))
                {
                    for (long i64Tmp = lStartNo; i64Tmp <= lEndNo; i64Tmp++)
                    {
                        strSQL = "INSERT INTO dbo.HxdNoTab(JCHxdDjTabID,PaidanRegisterTabID,HxdNo) VALUES(" + nJCHxdDjTabID.ToString() + "," + nPaidanRegisterTabID.ToString() + "," + i64Tmp.ToString() + ")";
                        arrayList.Add(strSQL);
                    }
                }
                else
                {
                    for (long i64Tmp = lStartNo; i64Tmp <= lEndNo; i64Tmp++)
                    {
                        strSQL ="UPDATE dbo.hxdnotab SET JCHxdDjTabID = '" + nJCHxdDjTabID.ToString() + "' " + " WHERE hxdno='" + i64Tmp.ToString() + "'";
                        arrayList.Add(strSQL);
                  
                    }
                }

                strSQL = "INSERT INTO dbo.QsZzHxdTab(JCHxdDjTabID,QsHxdNo,ZzHxdNo,HxdNum) VALUES(" + nJCHxdDjTabID.ToString() + "," + lStartNo.ToString() + "," + lEndNo.ToString() + "," + i64Cha.ToString() + ")";
                arrayList.Add(strSQL);

                strSQL = "UPDATE dbo.QsZzHxdTab SET HxdZNum=(SELECT SUM(HxdNum) FROM dbo.QsZzHxdTab WHERE JCHxdDjTabID=" + nJCHxdDjTabID.ToString() + ")" + " WHERE JCHxdDjTabID=" + nJCHxdDjTabID.ToString();
                arrayList.Add(strSQL);

                Frm_Main.Log2DB(this.Text, "增加", lStartNo.ToString() + "&" + lEndNo.ToString());
                DbHelperSQL.ExecuteSqlTran(arrayList);
                MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } 

            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        
        }

        private void InsertHxdNoTab(long nQsHxdNo, long nZzHxdNo, int nJCHxdDjTabID) 
        {
            int nPaidanRegisterTabID = 0;
            DataSet ds = m_bllPaidanRegisterTab.GetList("ContractCode = ''");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                nPaidanRegisterTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["PaidanRegisterTabID"].ToString(), 10);
            }
            else
            {
                MessageBox.Show("系统默认合同号不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            string strTempSql = "";
            for (long i64Tmp = nQsHxdNo; i64Tmp <= nZzHxdNo; i64Tmp++)
	        {
                strTempSql = "INSERT INTO dbo.HxdNoTab(JCHxdDjTabID,PaidanRegisterTabID,HxdNo) VALUES(" + nJCHxdDjTabID.ToString() + "," + nPaidanRegisterTabID.ToString() + "," + i64Tmp.ToString() + ")";

                m_UpdateList.Add(strTempSql);
	        }

        }

        private void DelHxdNoTab(long nQsHxdNo, long nZzHxdNo) 
        {
             string strTempSql = "DELETE FROM dbo.HxdNoTab WHERE HxdNo BETWEEN " + nQsHxdNo.ToString() +" AND "+ nZzHxdNo.ToString();
             m_UpdateList.Add(strTempSql);
        }

        private bool UpdateHxdNoTab(long i64OldQsHxdNo,long i64OldZzHxdNo,long i64QsHxdNo,long i64ZzHxdNo, int nJCHxdDjTabID) 
        {
            string strQsHxdNoJa1 = (i64QsHxdNo+1).ToString();
            string strQsHxdNoJian1 = (i64QsHxdNo-1).ToString();
            string strZzHxdNoJa1 = (i64ZzHxdNo+1).ToString();
            string strZzHxdNoJian1 = (i64ZzHxdNo-1).ToString();
            string strOldQsHxdNoJa1 = (i64OldQsHxdNo+1).ToString();
            string strOldQsHxdNoJian1 = (i64OldQsHxdNo-1).ToString();
            string strOldZzHxdNoJa1 = (i64OldZzHxdNo+1).ToString();
            string strOldZzHxdNoJian1 = (i64OldZzHxdNo-1).ToString();

            if (i64ZzHxdNo < i64OldQsHxdNo)//新的结束<旧的起始
            {
                DataSet ds = m_bllQsZzHxdTab.CheckHXDNo("HxdNo BETWEEN '" + i64QsHxdNo.ToString() + "'" + " AND '" + i64ZzHxdNo.ToString() + "'");

                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string strHxdNoTabIDCount = ds.Tables[0].Rows[0]["HxdNoTabIDCount"].ToString();
                    int nHxdNoTabIDCount = Convert.ToInt32(strHxdNoTabIDCount, 10);
                    if (nHxdNoTabIDCount > 0)
                    {
                        MessageBox.Show("核销单号重叠,请重新指定核销单号范围!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                    else
                    {
                        InsertHxdNoTab(i64QsHxdNo, i64ZzHxdNo, nJCHxdDjTabID);
                        DelHxdNoTab(i64OldQsHxdNo, i64OldZzHxdNo);
                    }
                }
            }
            else if (i64ZzHxdNo == i64OldQsHxdNo)//新的结束==旧的起始
            {
                DataSet ds = m_bllQsZzHxdTab.CheckHXDNo("HxdNo BETWEEN '" + i64QsHxdNo.ToString() + "'" + " AND '" + strZzHxdNoJian1 + "'");

                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string strHxdNoTabIDCount = ds.Tables[0].Rows[0]["HxdNoTabIDCount"].ToString();
                    int nHxdNoTabIDCount = Convert.ToInt32(strHxdNoTabIDCount, 10);
                    if (nHxdNoTabIDCount > 0)
                    {
                        MessageBox.Show("核销单号重叠,请重新指定核销单号范围!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                    else
                    {
                        InsertHxdNoTab(i64QsHxdNo, i64ZzHxdNo - 1, nJCHxdDjTabID);
                        DelHxdNoTab(i64OldQsHxdNo+1, i64OldZzHxdNo);
                    }
                }
            }
            else if ((i64ZzHxdNo > i64OldQsHxdNo) && (i64ZzHxdNo < i64OldZzHxdNo) && (i64QsHxdNo < i64OldQsHxdNo))//(新的结束>旧的起始)&&(新的结束<旧的结束)&&(新的起始<旧的起始)
            {
                DataSet ds = m_bllQsZzHxdTab.CheckHXDNo("HxdNo BETWEEN '" + i64QsHxdNo.ToString() + "'" + " AND '" + strOldQsHxdNoJian1 + "'");

                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string strHxdNoTabIDCount = ds.Tables[0].Rows[0]["HxdNoTabIDCount"].ToString();
                    int nHxdNoTabIDCount = Convert.ToInt32(strHxdNoTabIDCount, 10);
                    if (nHxdNoTabIDCount > 0)
                    {
                        MessageBox.Show("核销单号重叠,请重新指定核销单号范围!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                    else
                    {
                        InsertHxdNoTab(i64QsHxdNo, i64OldQsHxdNo - 1, nJCHxdDjTabID);
                        DelHxdNoTab(i64ZzHxdNo+1, i64OldZzHxdNo);
                    }
                }
            }
            else if ((i64ZzHxdNo > i64OldQsHxdNo) && (i64ZzHxdNo < i64OldZzHxdNo) && (i64QsHxdNo == i64OldQsHxdNo))//(新的结束>旧的起始)&&(新的结束<旧的结束)&&(新的起始==旧的起始)
            {
                DelHxdNoTab(i64ZzHxdNo+1, i64OldZzHxdNo);
            }
            else if ((i64ZzHxdNo > i64OldQsHxdNo) && (i64ZzHxdNo < i64OldZzHxdNo) && (i64QsHxdNo > i64OldQsHxdNo))//(新的结束>旧的起始)&&(新的结束<旧的结束)&&(新的起始>旧的起始)
            {
                DelHxdNoTab(i64OldQsHxdNo, i64QsHxdNo-1);
                DelHxdNoTab(i64ZzHxdNo+1, i64OldZzHxdNo);
            }
            else if ((i64ZzHxdNo == i64OldZzHxdNo) && (i64QsHxdNo < i64OldQsHxdNo))//(新的结束==旧的结束)&&(新的起始<旧的起始)
            {
                DataSet ds = m_bllQsZzHxdTab.CheckHXDNo("HxdNo BETWEEN '" + i64QsHxdNo.ToString() + "'" + " AND '" + strOldQsHxdNoJian1 + "'");

                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string strHxdNoTabIDCount = ds.Tables[0].Rows[0]["HxdNoTabIDCount"].ToString();
                    int nHxdNoTabIDCount = Convert.ToInt32(strHxdNoTabIDCount, 10);
                    if (nHxdNoTabIDCount > 0)
                    {
                        MessageBox.Show("核销单号重叠,请重新指定核销单号范围!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                    else
                    {
                        InsertHxdNoTab(i64QsHxdNo, i64OldQsHxdNo - 1, nJCHxdDjTabID);
                        
                    }
                }
            }
            else if ((i64ZzHxdNo == i64OldZzHxdNo) && (i64QsHxdNo > i64OldQsHxdNo))//(新的结束==旧的结束)&&(新的起始>旧的起始)
            {
                DelHxdNoTab(i64OldQsHxdNo, i64QsHxdNo-1);

            }

            else if ((i64ZzHxdNo > i64OldZzHxdNo) && (i64QsHxdNo < i64OldQsHxdNo))//(新的结束>旧的结束)&&(新的起始<旧的起始)
            {
                DataSet ds = m_bllQsZzHxdTab.CheckHXDNo("HxdNo BETWEEN '" + i64QsHxdNo.ToString() + "'" + " AND '" + strOldQsHxdNoJian1 + "'");

                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string strHxdNoTabIDCount = ds.Tables[0].Rows[0]["HxdNoTabIDCount"].ToString();
                    int nHxdNoTabIDCount = Convert.ToInt32(strHxdNoTabIDCount, 10);
                    if (nHxdNoTabIDCount > 0)
                    {
                        MessageBox.Show("核销单号重叠,请重新指定核销单号范围!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }

                ds = m_bllQsZzHxdTab.CheckHXDNo("HxdNo BETWEEN '" + strOldZzHxdNoJa1 + "'" + " AND '" + i64ZzHxdNo.ToString() + "'");

                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string strHxdNoTabIDCount = ds.Tables[0].Rows[0]["HxdNoTabIDCount"].ToString();
                    int nHxdNoTabIDCount = Convert.ToInt32(strHxdNoTabIDCount, 10);
                    if (nHxdNoTabIDCount > 0)
                    {
                        MessageBox.Show("核销单号重叠,请重新指定核销单号范围!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }

                InsertHxdNoTab(i64QsHxdNo, i64OldQsHxdNo - 1, nJCHxdDjTabID);
                InsertHxdNoTab(i64OldZzHxdNo + 1, i64ZzHxdNo, nJCHxdDjTabID);
                
            }

            else if ((i64ZzHxdNo > i64OldZzHxdNo) && (i64QsHxdNo == i64OldQsHxdNo))//(新的结束>旧的结束)&&(新的起始=旧的起始)
            {
                DataSet ds = m_bllQsZzHxdTab.CheckHXDNo("HxdNo BETWEEN '" + strOldZzHxdNoJa1 + "'" + " AND '" + i64ZzHxdNo.ToString() + "'");

                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string strHxdNoTabIDCount = ds.Tables[0].Rows[0]["HxdNoTabIDCount"].ToString();
                    int nHxdNoTabIDCount = Convert.ToInt32(strHxdNoTabIDCount, 10);
                    if (nHxdNoTabIDCount > 0)
                    {
                        MessageBox.Show("核销单号重叠,请重新指定核销单号范围!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                    else
                    {
                        InsertHxdNoTab(i64OldZzHxdNo + 1, i64ZzHxdNo, nJCHxdDjTabID);

                    }
                }
                
            }

            else if ((i64ZzHxdNo > i64OldZzHxdNo) && (i64QsHxdNo > i64OldQsHxdNo) && (i64QsHxdNo < i64OldZzHxdNo))//(新的结束>旧的结束)&&(新的起始>旧的起始)&&(新的起始<旧的结束)
            {
                DataSet ds = m_bllQsZzHxdTab.CheckHXDNo("HxdNo BETWEEN '" + strOldZzHxdNoJa1 + "'" + " AND '" + i64ZzHxdNo.ToString() + "'");

                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string strHxdNoTabIDCount = ds.Tables[0].Rows[0]["HxdNoTabIDCount"].ToString();
                    int nHxdNoTabIDCount = Convert.ToInt32(strHxdNoTabIDCount, 10);
                    if (nHxdNoTabIDCount > 0)
                    {
                        MessageBox.Show("核销单号重叠,请重新指定核销单号范围!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                    else
                    {
                        InsertHxdNoTab(i64OldZzHxdNo + 1, i64ZzHxdNo, nJCHxdDjTabID);
                        DelHxdNoTab(i64OldQsHxdNo, i64QsHxdNo-1);
                    }
                }
            }
            else if ((i64ZzHxdNo > i64OldZzHxdNo) && (i64QsHxdNo > i64OldQsHxdNo) && (i64QsHxdNo == i64OldZzHxdNo))//(新的结束>旧的结束)&&(新的起始>旧的起始)&&(新的起始==旧的结束)
            {
                DataSet ds = m_bllQsZzHxdTab.CheckHXDNo("HxdNo BETWEEN '" + strQsHxdNoJa1 + "'" + " AND '" + i64ZzHxdNo.ToString() + "'");

                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string strHxdNoTabIDCount = ds.Tables[0].Rows[0]["HxdNoTabIDCount"].ToString();
                    int nHxdNoTabIDCount = Convert.ToInt32(strHxdNoTabIDCount, 10);
                    if (nHxdNoTabIDCount > 0)
                    {
                        MessageBox.Show("核销单号重叠,请重新指定核销单号范围!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                    else
                    {
                        InsertHxdNoTab(i64QsHxdNo + 1, i64ZzHxdNo, nJCHxdDjTabID);
                        DelHxdNoTab(i64OldQsHxdNo, i64QsHxdNo-1);
                    }
                }
            }
            else if ((i64ZzHxdNo > i64OldZzHxdNo) && (i64QsHxdNo > i64OldQsHxdNo) && (i64QsHxdNo > i64OldZzHxdNo))//(新的结束>旧的结束)&&(新的起始>旧的起始)&&(新的起始>旧的结束)
            {
                DataSet ds = m_bllQsZzHxdTab.CheckHXDNo("HxdNo BETWEEN '" + i64QsHxdNo.ToString() + "'" + " AND '" + i64ZzHxdNo.ToString() + "'");

                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string strHxdNoTabIDCount = ds.Tables[0].Rows[0]["HxdNoTabIDCount"].ToString();
                    int nHxdNoTabIDCount = Convert.ToInt32(strHxdNoTabIDCount, 10);
                    if (nHxdNoTabIDCount > 0)
                    {
                        MessageBox.Show("核销单号重叠,请重新指定核销单号范围!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                    else
                    {
                        InsertHxdNoTab(i64QsHxdNo, i64ZzHxdNo, nJCHxdDjTabID);
                        DelHxdNoTab(i64OldQsHxdNo, i64OldZzHxdNo);
                    }
                }
            }
            return true;
        }
        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                m_UpdateList.Clear();
                if (IsNull(this.errorProvider1, this.textBoxStartNo))
                    return;

                long lStartNo = Convert.ToInt64(this.textBoxStartNo.Text, 10);
                long lEndNo = Convert.ToInt64(this.textBoxEndNo.Text, 10);
                long i64Cha = 0;
                if (lEndNo < lStartNo)
                {
                    MessageBox.Show("终止核销单号不能小于起始核销单号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    i64Cha = lEndNo - lStartNo + 1;
                    if (i64Cha > 2000)
                    {
                        MessageBox.Show("核销单号范围过大!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                long lStartNoOld = 0;
                long lEndNoOld = 0;
                int nJCHxdDjTabID = 0;
                DGBY.Model.QsZzHxdTab modelQsZzHxdTabTabOld = m_bllQsZzHxdTab.GetModel(m_nSelectQsZzHxdTabID);
                if (modelQsZzHxdTabTabOld != null)
                {
                    nJCHxdDjTabID = modelQsZzHxdTabTabOld.JCHxdDjTabID;

                    lStartNoOld = modelQsZzHxdTabTabOld.QsHxdNo;
                    lEndNoOld = modelQsZzHxdTabTabOld.ZzHxdNo;


                    bool bRet = UpdateHxdNoTab(lStartNoOld, lEndNoOld, lStartNo, lEndNo, modelQsZzHxdTabTabOld.JCHxdDjTabID);
                    if (bRet == false)
                        return;

                    DGBY.Model.JCHxdDjTab modelJCHxdDjTab = m_bllJCHxdDjTab.GetModel(nJCHxdDjTabID);
                    //modelJCHxdDjTab.JCHxdDjTabID = nJCHxdDjTabID;
                    if (this.comboBoxCompName.SelectedValue != null)
                        modelJCHxdDjTab.BYCompanyTabID = Convert.ToInt32(this.comboBoxCompName.SelectedValue.ToString(), 10);
                    if (this.comboBoxSeaGateName.SelectedValue != null)
                        modelJCHxdDjTab.HaiGuanTabID = Convert.ToInt32(this.comboBoxSeaGateName.SelectedValue.ToString(), 10);
                    if (this.comboBoxBGHName.SelectedValue != null)
                        modelJCHxdDjTab.BGHDataTabID = Convert.ToInt32(this.comboBoxBGHName.SelectedValue.ToString(), 10);
                    if (this.comboBoxPoster.SelectedValue != null)
                        modelJCHxdDjTab.UserPasswordTabID = Convert.ToInt32(this.comboBoxPoster.SelectedValue.ToString(), 10);

                    modelJCHxdDjTab.JCDZDate = this.dateTimePickerSheetPostDate.Value;
                    modelJCHxdDjTab.JCDZDate = modelJCHxdDjTab.JCDZDate.Date;
                    m_bllJCHxdDjTab.Update(modelJCHxdDjTab);

                    string strSQL3 = "UPDATE dbo.QsZzHxdTab SET QsHxdNo=" + lStartNo.ToString() + ",ZzHxdNo=" + lEndNo.ToString()
                        + ",HxdNum=" + i64Cha.ToString() + " WHERE QsHxdNo=" + lStartNoOld.ToString();
                    m_UpdateList.Add(strSQL3);

                    strSQL3 = "UPDATE dbo.QsZzHxdTab SET HxdZNum=(SELECT SUM(HxdNum) FROM dbo.QsZzHxdTab WHERE JCHxdDjTabID=" + nJCHxdDjTabID.ToString() + ")" + " WHERE JCHxdDjTabID=" + nJCHxdDjTabID.ToString();
                    m_UpdateList.Add(strSQL3);

                    DbHelperSQL.ExecuteSqlTran(m_UpdateList);
                    Frm_Main.Log2DB(this.Text, "修改", lStartNo.ToString() + "&" + lEndNo.ToString());
                    ShowAllRecordInDataGridView("");
                    MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

       

        
        private void buttonDel_Click(object sender, EventArgs e)
        {
            DGBY.Model.QsZzHxdTab modelQsZzHxdTabTabOld = m_bllQsZzHxdTab.GetModel(m_nSelectQsZzHxdTabID);
            if (modelQsZzHxdTabTabOld == null)
            {
                MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            int nJCHxdDjTabID = modelQsZzHxdTabTabOld.JCHxdDjTabID;

            DialogResult dret = MessageBox.Show("删除记录将会影响到与之相关的其它记录,是否真的删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (dret == DialogResult.No)
                return;

            List<string> arrayList = new List<string>();
            string strSQL = "";

            long lStartNo = Convert.ToInt64(this.textBoxStartNo.Text, 10);
            long lEndNo = Convert.ToInt64(this.textBoxEndNo.Text, 10);
            long i64Cha = lEndNo - lStartNo;

            strSQL = "DELETE FROM dbo.QsZzHxdTab WHERE QsHxdNo=" + lStartNo.ToString();
            arrayList.Add(strSQL);


            strSQL = "UPDATE dbo.QsZzHxdTab SET HxdZNum=(SELECT SUM(HxdNum) FROM dbo.QsZzHxdTab WHERE JCHxdDjTabID=" + nJCHxdDjTabID.ToString() + ")" + " WHERE JCHxdDjTabID=" + nJCHxdDjTabID.ToString();
            arrayList.Add(strSQL);

            strSQL = "DELETE FROM dbo.HxdNoTab WHERE HxdNo BETWEEN " + lStartNo.ToString() + " AND " + lEndNo.ToString();
            arrayList.Add(strSQL);

            DataSet ds = m_bllQsZzHxdTab.CheckJCHxdDjTabID("JCHxdDjTabID=" + nJCHxdDjTabID);
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                string strHxdNoTabIDCount = ds.Tables[0].Rows[0]["QsZzHxdTabIDCount"].ToString();
                int nHxdNoTabIDCount = Convert.ToInt32(strHxdNoTabIDCount, 10);
                if (nHxdNoTabIDCount == 0)
                {
                    strSQL = "DELETE FROM dbo.JCHxdDjTab WHERE JCHxdDjTabID=" + nJCHxdDjTabID.ToString();
                    arrayList.Add(strSQL);
                }
            }

            DbHelperSQL.ExecuteSqlTran(arrayList);

            Frm_Main.Log2DB(this.Text, "删除", lStartNo.ToString() + "&" + lEndNo.ToString());
            ShowAllRecordInDataGridView("");
            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void cellQsZzHxdTabClick()
        {
            DGBY.Model.QsZzHxdTab modelQsZzHxdTabTab = m_bllQsZzHxdTab.GetModel(m_nSelectQsZzHxdTabID);
            if (modelQsZzHxdTabTab != null)
            {
                DGBY.Model.JCHxdDjTab modelJCHxdDjTab = m_bllJCHxdDjTab.GetModel(modelQsZzHxdTabTab.JCHxdDjTabID);
                if (modelJCHxdDjTab != null)
                {
                    if (modelJCHxdDjTab.BYCompanyTabID != null)
                        this.comboBoxCompName.SelectedValue = modelJCHxdDjTab.BYCompanyTabID;
                    if (modelJCHxdDjTab.HaiGuanTabID != null)
                        this.comboBoxSeaGateName.SelectedValue = modelJCHxdDjTab.HaiGuanTabID;
                    if (modelJCHxdDjTab.BGHDataTabID != null)
                        this.comboBoxBGHName.SelectedValue = modelJCHxdDjTab.BGHDataTabID;
                    if (modelJCHxdDjTab.UserPasswordTabID != null)
                        this.comboBoxPoster.SelectedValue = modelJCHxdDjTab.UserPasswordTabID;

                    this.dateTimePickerSheetPostDate.Value = (System.DateTime)modelJCHxdDjTab.JCDZDate;
                }

                this.textBoxStartNo.Text = modelQsZzHxdTabTab.QsHxdNo.ToString();
                this.textBoxEndNo.Text = modelQsZzHxdTabTab.ZzHxdNo.ToString();
                this.textBoxSheetCount.Text = modelQsZzHxdTabTab.HxdNum.ToString();
                
            }
            else
            {
                //this.comboBoxStartNo.Text = "";
                //this.comboBoxEndNo.Text = "";
                this.textBoxSheetCount.Text = "";
            }


        }



        private void ShowAllRecordInDataGridView(string strWhere)
        {

            m_ds = m_bllQsZzHxdTab.GetPostRegList(strWhere);
            ultraGrid1.DataSource = null;

            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = m_ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns[1].Hidden = true;
            }
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView("");
        }

      
       
        private bool IsNum(string strInput)
        {
            return Regex.IsMatch(strInput, @"^[1-9]+\d*$");
        }

        private bool ValidDate(string strInput)
        {
            if (strInput.Length != 8)
                return false;
            else
                return IsNum(strInput);
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }


        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;

            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }
        public static void ultraGrid1_InitializeRowFilter(Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            //ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            //foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //    ugc.PerformAutoResize();
            // FILTER ROW FUNCTIONALITY RELATED ULTRAGRID SETTINGS
            //e.Layout.Override.RowFilterAction = RowFilterAction.DisableFilteredOutRows;
            // ----------------------------------------------------------------------------------
            // Enable the the filter row user interface by setting the FilterUIType to FilterRow.
            e.Layout.Override.FilterUIType = FilterUIType.FilterRow;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // FilterEvaluationTrigger specifies when UltraGrid applies the filter criteria typed 
            // into a filter row. Default is OnCellValueChange which will cause the UltraGrid to
            // re-filter the data as soon as the user modifies the value of a filter cell.
            e.Layout.Override.FilterEvaluationTrigger = FilterEvaluationTrigger.OnCellValueChange;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // By default the UltraGrid selects the type of the filter operand editor based on
            // the column's DataType. For DateTime and boolean columns it uses the column's editors.
            // For other column types it uses the Combo. You can explicitly specify the operand
            // editor style by setting the FilterOperandStyle on the override or the individual
            // columns.

            e.Layout.Override.FilterOperandStyle = FilterOperandStyle.DropDownList;

            // By default UltraGrid displays user interface for selecting the filter operator. 
            // You can set the FilterOperatorLocation to hide this user interface. This
            // property is available on column as well so it can be controlled on a per column
            // basis. Default is WithOperand. This property is exposed off the column as well.
            e.Layout.Override.FilterOperatorLocation = FilterOperatorLocation.WithOperand;

            // By default the UltraGrid uses StartsWith as the filter operator. You use
            // the FilterOperatorDefaultValue property to specify a different filter operator
            // to use. This is the default or the initial filter operator value of the cells
            // in filter row. If filter operator user interface is enabled (FilterOperatorLocation
            // is not set to None) then that ui will be initialized to the value of this
            // property. The user can then change the operator as he/she chooses via the operator
            // drop down.
            e.Layout.Override.FilterOperatorDefaultValue = FilterOperatorDefaultValue.StartsWith;

            // FilterOperatorDropDownItems property can be used to control the options provided
            // to the user for selecting the filter operator. By default UltraGrid bases 
            // what operator options to provide on the column's data type. This property is
            // avaibale on the column as well.
            //e.Layout.Override.FilterOperatorDropDownItems = FilterOperatorDropDownItems.All;

            // By default UltraGrid displays a clear button in each cell of the filter row
            // as well as in the row selector of the filter row. When the user clicks this
            // button the associated filter criteria is cleared. You can use the 
            // FilterClearButtonLocation property to control if and where the filter clear
            // buttons are displayed.
            e.Layout.Override.FilterClearButtonLocation = FilterClearButtonLocation.RowAndCell;

            // Appearance of the filter row can be controlled using the FilterRowAppearance proeprty.
            e.Layout.Override.FilterRowAppearance.BackColor = Color.LightYellow;

            // You can use the FilterRowPrompt to display a prompt in the filter row. By default
            // UltraGrid does not display any prompt in the filter row.
            e.Layout.Override.FilterRowPrompt = "Click here to filter data...";

            // You can use the FilterRowPromptAppearance to change the appearance of the prompt.
            // By default the prompt is transparent and uses the same fore color as the filter row.
            // You can make it non-transparent by setting the appearance' BackColorAlpha property 
            // or by setting the BackColor to a desired value.
            e.Layout.Override.FilterRowPromptAppearance.BackColorAlpha = Alpha.Opaque;

            // By default the prompt is spread across multiple cells if it's bigger than the
            // first cell. You can confine the prompt to a particular cell by setting the
            // SpecialRowPromptField property off the band to the key of a column.
            //e.Layout.Bands[0].SpecialRowPromptField = e.Layout.Bands[0].Columns[0].Key;

            // Display a separator between the filter row other rows. SpecialRowSeparator property 
            // can be used to display separators between various 'special' rows, including for the
            // filter row. This property is a flagged enum property so it can take multiple values.
            e.Layout.Override.SpecialRowSeparator = SpecialRowSeparator.FilterRow;

            // You can control the appearance of the separator using the SpecialRowSeparatorAppearance
            // property.
            e.Layout.Override.SpecialRowSeparatorAppearance.BackColor = Color.FromArgb(233, 242, 199);

            //   UltraGridBand band = e.Layout.Bands[0];
            //   band.ColumnFilters["原币金额"].FilterConditions.Clear();
            //   band.ColumnFilters["原币金额"].FilterConditions.Add(

            //FilterComparisionOperator.GreaterThan, 5);
            //   band.ColumnFilters["原币金额"].FilterConditions.Add(

            //FilterComparisionOperator.LessThan, 10);
            //   band.ColumnFilters["原币金额"].LogicalOperator = FilterLogicalOperator.And;
        }
        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            if (this.checkBox1.Checked == true)
                ultraGrid1_InitializeRowFilter(e);
            else
                ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);

            ClassUltraGridCommonSet.LocalPrintPreveiw();
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            string strQsZzHxdTabID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectQsZzHxdTabID = Convert.ToInt32(strQsZzHxdTabID, 10);
            cellQsZzHxdTabClick();
        }

        private void Frm_PostToBGSheetReg_Activated(object sender, EventArgs e)
        {
            InitCombox();
        }

        private void ultraGridExcelExporter1_CellExported(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.CellExportedEventArgs e)
        {
            Worksheet ws = e.CurrentWorksheet;
            int iRdex = e.CurrentRowIndex;
            int iCdex = e.CurrentColumnIndex;
            //e.CurrentWorksheet.Rows[iRdex].Cells[iCdex].CellFormat.FillPatternForegroundColor = Color.FromArgb(0, 0, 0); 
            // string strColumnName = e.GridColumn;
            if (e.GridColumn.Key == "起始的核销单号" || e.GridColumn.Key == "终止的核销单号")
            {
                //m_ugr[iRdex-1].Cells[e.GridColumn.Key + "公式"].Value.ToString()
                string strNo = e.CurrentWorksheet.Rows[iRdex].Cells[iCdex].Value.ToString();
                int nNo = Convert.ToInt32(strNo, 10);
                if(nNo < 100000000)
                    e.CurrentWorksheet.Rows[iRdex].Cells[iCdex].Value = "0" + e.CurrentWorksheet.Rows[iRdex].Cells[iCdex].Value;
            }
        }

    }
}